package com.happyfamily.springboot.crowdsourcingplatform.service.wang.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.wang.ExposureDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Exposure;
import com.happyfamily.springboot.crowdsourcingplatform.model.Orders;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.ExposureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static io.netty.resolver.HostsFileParser.parse;


//曝光台实现类
@Service
@Transactional
@Slf4j
public class ExposureServiceImpl implements ExposureService
{
	@Autowired
	private ExposureDao exposureDao;

	//前端轮播图
	@Override
	public List<Exposure> findExposure()
	{
		return exposureDao.findExposure();
	}

	//前端查看更多
	@Override
	public Results<Exposure> findExposureList(Integer startPosition, Integer limit, String who, String starttime, String endtime,String exposureSticky)
	{
		return Results.success(exposureDao.ExposureCount(who,starttime,endtime,exposureSticky).intValue(),exposureDao.findExposureList(startPosition,limit,who,starttime,endtime,exposureSticky));
	}

	//后台管理员曝光台管理
	@Override
	public Results<Exposure> exposureDeploy(Integer startPosition, Integer limit, String who, String starttime, String endtime, String exposureType,String exposureSticky)
	{
		return Results.success(exposureDao.exposureDeployCount(who,starttime,endtime,exposureType,exposureSticky).intValue(),exposureDao.exposureDeployList(startPosition,limit,who,starttime,endtime,exposureType,exposureSticky));

	}

	//曝光台删除
	@Override
	public String exposureDelete(String eid)
	{
		String msg="error";
		int index=exposureDao.exposureDetele(eid);
		if (index>0)
		{
			msg="success";
		}
		return msg;
	}

	//后台管理员对置顶状态的修改
	@Override
	public String exposureSticky(String eid, String sticky)
	{
		String res="error";
		int index=exposureDao.exposureSticky(eid,sticky);
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//仲裁审核通过
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String exposureYes(String eid, String oid)
	{
		String msg="error";
		SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
		String starttime=s.format(new Date());
		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.DAY_OF_MONTH,curr.get(Calendar.DAY_OF_MONTH)+7);
		String endtime=s.format(curr.getTime());
		int index=exposureDao.exposureYes(eid,starttime,endtime);
		if (index>0)
		{
			index=exposureDao.updataStatus(oid,"7");
			if (index>0)
			{
				index=exposureDao.arbitrationStatus(oid,"7");
				if (index>0)
				{
					msg="success";
				}
			}
		}
		return msg;
	}

	//仲裁审核不通过
	@Transactional(rollbackFor=Exception.class)
	@Override
	public String exposureNo(String eid, String oid)
	{
		String msg="error";
		int index=exposureDao.exposureDetele(eid);
		if (index>0)
		{
			index=exposureDao.updataStatus(oid,"4");
			if (index>0)
			{
				msg="success";
			}
		}
		return msg;
	}

	//曝光订单详情查看
	@Override
	public Orders exposureDetails(String oid)
	{
		Orders o=exposureDao.exposureDetails(oid);
		String uname=exposureDao.searchName(o.getNuseraccount());
		o.setUname(uname);
		return o;
	}
}

